package bookTrading.seller;

import java.text.DecimalFormat;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

@SuppressWarnings("serial")
public class SellerAcceptProposal extends CyclicBehaviour
{
	private static Boolean acordado = false;
	private AgentSeller agentSeller;


	// Construtor
	public SellerAcceptProposal(AgentSeller agentSeller)
	{
		this.agentSeller = agentSeller;
	}


	@Override
	public void action()
	{
		MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
		ACLMessage msg = agentSeller.receive(mt);

		if (msg != null)
		{
			ACLMessage reply = msg.createReply();
			
			if (!acordado)
			{
				Double valorAcordado = Double.parseDouble(msg.getContent());
	
				DecimalFormat df = new DecimalFormat("#,###.00");
				System.out.println("[AcceptProposal] Acordo efetuado, valor de Venda: R$ " + df.format(valorAcordado));
	
				reply.setPerformative(ACLMessage.ACCEPT_PROPOSAL);
				reply.setContent(String.valueOf(valorAcordado));
	
				acordado = true;
			}
			else
			{
				reply.setPerformative(ACLMessage.INFORM);
				reply.setContent("Obrigado pela Compra!");
			}

			agentSeller.send(reply);
		}
	}
}